<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$VIEW->setTitle('Page List');

$VIEW->addTemplate('main', 'admin/source-browser.tpl');


$res = ini_set('memory_limit', "64M") or die('Error');



if(get('get-file-content', '') !== ''){
    $reqFile = get('get-file-content');
    $reqFile = str_replace(array('../', DIRECTORY_SEPARATOR, '//'), '/', $reqFile);

    $validFile = false;
    if(strpos($reqFile, $CONFIG->baseDir) === 0) $validFile = true;
    if(strpos($reqFile, $CONFIG->publicDir) === 0) $validFile = true;

    if($validFile && file_exists($reqFile)){
        while(ob_get_level()>0) ob_end_clean();
        $fc = file_get_contents($reqFile);

        $fileTypes = array(
            'js' => 'javascript',
            'css' => 'css',
            'php' => 'php'
        );

        $finfo = pathinfo($reqFile);
        if(!array_key_exists(strtolower($finfo['extension']), $fileTypes)) continue;
        $GESHI = new GeSHi($fc, strtolower($fileTypes[$finfo['extension']]));
        $GESHI->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 3);
        echo $GESHI->parse_code();

        exit;
    }else{
        die($reqFile.' is invalid');
    }
}





$css = <<<CSS
#jfxbrowseclose{
    position: absolute;
    right: 4px;
    top: 4px;
    cursor: pointer;
}
.folder{
    float: left;
    clear: left;
    width: 100%;
}
.folder-content{
    float: left;
    width: 98%;
    padding-left: 1%;
    border-left: solid 1px #AAB1B1;
    margin-left: 1%;
    margin-top: 8px;
    margin-bottom: 20px;
}
/*.folder > .folder-content{
    display: none;
}*/
.folder-title{
    cursor: pointer;
}
.folder img, .file img{
    margin-right: 4px;
}
.tabbertab-wrapper{
    padding: 24px;
}

#file-content{
    position: absolute;
    left: 0;
    top: 0;
    background: white;
    border: solid 1px #CCC;
    padding: 24px;
    color: gray;
    width: 800px;
    text-align: left;
    font-family: courier, monospace;
    overflow: scroll;
    z-index: 250;
}
.file-content-btn{
    cursor: pointer;
}
.file{
    clear: left;
    float: left;
    width: 100%;
}
.file-title{
    float: left;
}
.icon-string{
    float: right;
    min-width: 80px;
}
#content #main .tabbernav li{
    width: 33% !important;
}
#content #main .tabbertab{
    width: 964px !important;
}

CSS;

    $VIEW->addCss($css);

$baseUrl = $CONFIG->baseUrl;

$js = <<<JS


var showFunc = function(txt){
    var txtPre = '<img src="{$CONFIG->imageUrl}/admin/icons/close-gray-normal.png" alt="close" id="jfxbrowseclose" />';
    txt = txtPre+txt;
    $('#file-content').html(txt);
    $('#jfxbrowseclose').hover(function(){
        $(this).attr('src', $(this).attr('src').replace('normal', 'hover'));
    }, function(){
        $(this).attr('src', $(this).attr('src').replace('hover', 'normal'));
    });
    $('#jfxbrowseclose').click(function(){
        $('#file-content').animate({'opacity' : 0, 'left' : -1000}, 800);
    });
}

function getY( oElement ){
    var iReturnValue = 0;
    while( oElement != null ) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getX(oElement){
    var iReturnValue = 0;
    while( oElement != null ) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}



$(document).ready(function(){

// create our file content div
var div = document.createElement('div');
div.id = 'file-content';
$('body').append(div);
$(div).css('opacity', 0);


$('.folder-title').click(function(){
    if($('> .folder-content', this.parentNode).css('display') == 'none'){
        // show it
        $('> .folder-content', this.parentNode).slideDown();
    }else{
        // hide it
        $('> .folder-content', this.parentNode).slideUp();
    }
});

$('.folder > .folder-content').css('display', 'none');


$('.file-content-btn').click(function(){
    $('#file-content').html('');
    var img = document.createElement('img');
    img.src = '/images/site/icons/loading.gif';
    img.alt = "Loading";
    $('#file-content').append(img);
    var filename = $(this.parentNode).attr('filename');

    var left = getX(this);
    var top = getY(this.parentNode);
    $('#file-content').css('left', left-800);
    $('#file-content').css('top', top);
    $('#file-content').animate({'opacity' : 1}, 800);

    $.get('{$CONFIG->adminUrl}/source-browser/', {'get-file-content' : filename}, showFunc);
});


});




JS;

    $VIEW->addJs($js);

















function writeFolders($path='', $depth=0){
    global $numFiles;
    $validExts = array(
        'php',
        'js',
        'css',
        'jpg',
        'gif',
        'png'
    );
    $CONFIG = JFX::registry('config');
    $dh = opendir($path);
    $dirs = array();
    $files = array();
    while($file = readdir($dh)){

        if($file == '.' || $file == '..') continue;

        $allowedFolder = true;
        if($file=='tiny_mce' || $file=='cache' || $file=='tmp' || $file=='codepress' || $file=='install') $allowedFolder = false;

        if(is_dir($path.'/'.$file)){
            ob_start();

            if($allowedFolder) writeFolders($path.'/'.$file, $depth+1);
            else echo '-Empty Directory-';

            $content = ob_get_contents();
            ob_end_clean();
            $dirs[] = array(
                'name' => $file,
                'path' => $path,
                'full_path' => $path.'/'.$file,
                'type' => 'folder',
                'contents' => $content
            );
        }else{
            $finfo = pathinfo($path.'/'.$file);
            if(!in_array(strtolower($finfo['extension']), $validExts)) continue;
            $numFiles++;
            $files[] = array(
                'name' => $file,
                'path' => $path,
                'full_path' => $path.'/'.$file,
                'type' => 'file',
                'contents' => ''
            );
        }
    }
    $output = '';
    
    foreach($dirs as $k=>$v){




        $output .= '<div class="folder">';// str_repeat('<div class="folder" style="padding-left: 16px">', $depth+1);

        $output .= '<div class="folder-title"><img src="'.$CONFIG->imageUrl.'/admin/icons/folder.png" />'.$v['name'].'</div>';

        $output .= '<div class="folder-content">'.$v['contents'].'</div>';

        $output .= '</div>';//str_repeat('</div>', $depth+1);
    }

    if(count($files) == 0){
        $output .= '<div class="file-title"> - Empty Directory - </div>';
    }


    foreach($files as $k=>$v){
        $numFiles++;
        $iconString = '';

        $iconString .= '<img src="'.$CONFIG->imageUrl.'/admin/icons/show.png" title="View Source" class="file-content-btn" />';

        $output .= '<div class="file">';
        $output .= '<div class="icon-string" filename="'.$v['full_path'].'">'.$iconString.'</div>';
        $output .= '<div class="file-title">
                    <img src="'.$CONFIG->imageUrl.'/admin/icons/file.png" />'.$v['name'].'</div>';
        $output .= '</div>';

    }

    echo $output;
    //echo number_format(memory_get_usage(), 2, '.', ',');

}


// do our admin pages

global $numFiles;
$numFiles = 0;
ob_start();
writeFolders($CONFIG->baseDir);
$res = ob_get_contents();
ob_end_clean();

$SMARTY->assign('numAdminFiles', $numFiles);
$SMARTY->assign('adminResultText', $res);



// do our public pages
$numFiles = 0;
ob_start();
writeFolders($CONFIG->publicDir.'/jfx');
$res = ob_get_contents();
ob_end_clean();

$SMARTY->assign('numPublicFiles', $numFiles);
$SMARTY->assign('publicResultText', $res);







